package com.isunland.managesystem.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.ProjectOriginal;
import com.isunland.managesystem.entity.RProjectListMain;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lenovo on 2016/6/1.
 */
public class SingleProjectListFragment extends BaseListFragment {
  private BaseListAdapter mAdapter;

  @Override public String getUrl() {
    return ApiConst.URL_PROJECT_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    String memberCode = CurrentUser.newInstance(getActivity()).getMemberCode();
    params.put("curPage", "1");
    params.put("pageSize", "10000");
    params.put("memberCode", memberCode);
    params.put("IfAcceptance", "F");
    params.put("ifAllpay", "F");
    params.put("dataStatus", "publish");
    params.put("orderField", "order_no");
    params.put("orderSeq", "desc");
    params.put("type", "mobile");

    return params;
  }

  @Override public void resolveResponse(String arg0) {

    ProjectOriginal attendanceListOriginal = new Gson().fromJson(arg0, ProjectOriginal.class);

    List<RProjectListMain> rows = attendanceListOriginal.getRows();
    setAdapter(rows);
  }

  private void setAdapter(List<RProjectListMain> detail) {
    if (detail == null) {
      detail = new ArrayList<RProjectListMain>();
    }

    if (mAdapter == null) {
      mAdapter = new BaseListAdapter<RProjectListMain>(getActivity(), detail) {

        @SuppressLint("NewApi") @Override
        public void bindData(int position, BaseListAdapter.ViewHolder vh, RProjectListMain item) {
          vh.mTitleOneTv.setText(getContext().getString(R.string.projectName));
          vh.mContentOneTv.setText(item.getProjectName());
          vh.mTitleTwoTv.setText(getContext().getString(R.string.customerName));
          vh.mContentTwoTv.setText(item.getPartaName());
        }
      };
      setListAdapter(mAdapter);
      return;
    }

    mAdapter.clear();
    mAdapter.addAll(detail);
    mAdapter.notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    RProjectListMain item = (RProjectListMain) mAdapter.getItem(position - 1);
    CustomerDialog content =
        new CustomerDialog(item.getProjectName(), item.getId(), item.getContractAmount() + "", "");
    Intent i = new Intent();
    i.putExtra(ContractChoosePagerActivity.EXTRA_CONTRACT, content);
    i.putExtra(ContractChoosePagerActivity.EXTRA_TYPE, ContractChoosePagerActivity.PROJECT);
    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();

    super.onListItemClick(l, v, position, id);
  }

  public void refresh() {
    volleyPost();
  }
}

